package com.huarun.module.timer;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;
import org.springframework.stereotype.Component;

import com.huarun.common.util.PropertyUtil;

/**
 * @author dzm
 * @创建时间 2018-11-02 18:01
 * @Description 类描述
 * 
 */
@Component

public class SchedulerAllJob {
	@Autowired
	private SchedulerFactoryBean schedulerFactoryBean;

	/* 
	 * 调用定时任务
	 */
	public void scheduleDayJobs() throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		scheduleDayJob(scheduler);
	}

	/** 
	 * 配置定时任务
	 * @param scheduler 
	 * @throws SchedulerException 
	 */
	private void scheduleDayJob(Scheduler scheduler) throws SchedulerException {
		//ScheduledDayJob为具体执行的内容
		JobDetail jobDetailB = JobBuilder.newJob(ScheduledBDayJob.class).withIdentity("day1", "group1").build();
		JobDetail jobDetailC = JobBuilder.newJob(ScheduledCDayJob.class).withIdentity("day2", "group2").build();
		JobDetail jobDetailD = JobBuilder.newJob(ScheduledDDayJob.class).withIdentity("day3", "group3").build();
		JobDetail jobDetailA = JobBuilder.newJob(ScheduledADayJob.class).withIdentity("day4", "group4").build();
		//设置定时规则 B座
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(PropertyUtil.getProperty("faceBdayTimer"));
		CronTrigger cronTrigger = TriggerBuilder.newTrigger().withIdentity("triggerDay1", "group1")
				.withSchedule(scheduleBuilder).build();
		scheduler.scheduleJob(jobDetailB, cronTrigger);
		
		
		//设置定时规则  C座
		CronScheduleBuilder scheduleBuilderC = CronScheduleBuilder.cronSchedule(PropertyUtil.getProperty("faceCdayTimer"));
		CronTrigger cronTriggerC = TriggerBuilder.newTrigger().withIdentity("triggerDay2", "group2")
				.withSchedule(scheduleBuilderC).build();
		scheduler.scheduleJob(jobDetailC, cronTriggerC);
		
		
		//设置定时规则  D座
		CronScheduleBuilder scheduleBuilderD = CronScheduleBuilder.cronSchedule(PropertyUtil.getProperty("faceDdayTimer"));
		CronTrigger cronTriggerD = TriggerBuilder.newTrigger().withIdentity("triggerDay3", "group3")
				.withSchedule(scheduleBuilderD).build();
		scheduler.scheduleJob(jobDetailD, cronTriggerD);
		// 春笋 华润大厦
		CronScheduleBuilder scheduleBuilderA = CronScheduleBuilder.cronSchedule(PropertyUtil.getProperty("faceAdayTimer"));
		CronTrigger cronTriggerA = TriggerBuilder.newTrigger().withIdentity("triggerDay4", "group4")
				.withSchedule(scheduleBuilderA).build();
		scheduler.scheduleJob(jobDetailA, cronTriggerA);
	}

	/**
	 * 暂停所有任务
	 * 
	 * @throws SchedulerException
	 */
	public void pauseAllJob() throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		scheduler.pauseAll();
	}

	/**
	* 暂停某个任务
	* 
	* @param name
	* @param group
	* @throws SchedulerException
	*/
	public void pauseJob(String name, String group) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = new JobKey(name, group);
		JobDetail jobDetail = scheduler.getJobDetail(jobKey);
		if (jobDetail == null)
			return;
		scheduler.pauseJob(jobKey);
	}

	/**
	* 恢复所有任务
	* 
	* @throws SchedulerException
	*/
	public void resumeAllJob() throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		scheduler.resumeAll();
	}

	/**
	* 恢复某个任务
	* 
	* @param name
	* @param group
	* @throws SchedulerException
	*/
	public void resumeJob(String name, String group) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = new JobKey(name, group);
		JobDetail jobDetail = scheduler.getJobDetail(jobKey);
		if (jobDetail == null)
			return;
		scheduler.resumeJob(jobKey);
	}

	/**
	* 删除某个任务
	* 
	* @param name
	* @param group
	* @throws SchedulerException
	*/
	public void deleteJob(String name, String group) throws SchedulerException {
		Scheduler scheduler = schedulerFactoryBean.getScheduler();
		JobKey jobKey = new JobKey(name, group);
		JobDetail jobDetail = scheduler.getJobDetail(jobKey);
		if (jobDetail == null)
			return;
		scheduler.deleteJob(jobKey);
	}
	
	
	public static void main(String[] args) {
		System.out.println(PropertyUtil.getProperty("faceDdayTimer"));
	}

}
